/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.event;

import hope.platform.data.domain.SiteInfo;
import hope.platform.data.domain.Warning;
import hope.platform.data.domain.WarningKind;
import hope.platform.data.domain.entity.RelationPeople;
import hope.platform.data.server.data.header.SiteInfoService;
import hope.platform.data.server.data.header.WarningKindService;
import hope.platform.data.server.data.header.WarningService;
import hope.platform.data.server.function.header.EmailService;
import hope.platform.data.server.function.header.MessageService;
import hope.platform.data.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/24 16:44
 */
@Component
@Slf4j
public class WarningListener {

    @Autowired
    private WarningService warningService;

    @Autowired
    private WarningKindService warningKindService;

    @Autowired
    private SiteInfoService siteInfoService;

    @Autowired
    private EmailService emailService;

    @Autowired
    private MessageService messageService;

    @Async
    @EventListener
    public void listenerEvent(WarningEvent warningEvent) {
        informWarning(warningEvent);
    }

    public void informWarning(WarningEvent warningEvent) {
        Date now = new Date();
        WarningKind warningKind = warningKindService.getByWarningType(warningEvent.getWarning().getWarningType());
        Date beginTime = TimeUtils.getBeforeTime(now, warningKind.getInterval());
        //判断警告能否发出
        //查询警告数据库规定时间内是否有警告发出
        long count = warningService.countBySerialNumberAndDataNameAndGmtCreateBetween(
                warningEvent.getWarning().getSerialNumber(),
                warningEvent.getWarning().getDataName(),
                beginTime,
                now);
        if (count == 0) {
            warningImmediate(warningEvent.getWarning(), warningKind);
        } else {
            log.warn("there is a warning,but is non-conform to the regulations");
        }
    }

    private void warningImmediate(Warning warning, WarningKind warningKind) {
        //是否获取站点相关人员信息
        if (warningKind.getMethod().getEmail() == 1 || warningKind.getMethod().getEmail() == 1) {
            //哪些人员收警报
            SiteInfo siteInfo = siteInfoService.getSiteInfoBySerialNumber(warning.getSerialNumber());
            Map<String, RelationPeople> relationPeoples = siteInfo.getPeopleRelation();
            for (Map.Entry<String, RelationPeople> entry : relationPeoples.entrySet()) {
                if (warningKind.getMethod().getEmail() == 1 && entry.getValue().getEmail() != null) {
                    emailService.sendSimpleMail(entry.getValue().getEmail(),
                            "浩谱平台" + warningKind.getWarningType(), warning.getContent());
                }
                if (warningKind.getMethod().getMessage() == 1 && entry.getValue().getPhone() != null) {
                    //暂时关闭短信功能。
//                    messageService.sendMessage(entry.getValue().getPhone(),entry.getValue().getName(),
//                            warningKind.getWarningType());
                }
            }
        }
        //警报入库
        warningService.addWarning(warning);
    }
}
